﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Server.TcpIp;
using SensorClient;
using System.Xml.Serialization;
using System.IO;

namespace Server
{
    public partial class TcpIpForm : Form
    {
        private TcpServerModel server;
        private const int DEFAULT_PORT = 3000;

        public TcpIpForm()
        {
            InitializeComponent();
          
        }

        private void server_MsgReceived(object sender, MsgReceivedEventArgs e)
        {
            if (txtLog.InvokeRequired == true)
            {
                    txtLog.Invoke(new MethodInvoker(delegate {
                        SensorContainer sc = SensorContainer.FromXmlString(e.Message);
                        txtLog.Clear();
                        txtLog.Text = sc.ToString();
                }));
            }
        }

        private void butStart_Click(object sender, EventArgs e)
        {
            try
            {
                server = new TcpServerModel(Convert.ToInt32(txtPort.Text));
                server.Start();
                server.MsgReceived += new MsgReceivedEventHandler(server_MsgReceived);
                butStart.Enabled = false;
                txtPort.Enabled = false;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: "+ex.Message);
            }
        }
    }
}
